package com.tc.kqdemo.service;

import com.alibaba.fastjson.JSON;
import com.tc.kqdemo.pojo.Data;
import com.tc.kqdemo.pojo.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DataService {
    @Autowired
    private JdbcTemplate jdbcTemplate;


    //保存接收的数据
    public boolean saveData(Data d){
        String sql="insert into dd_data(userid,name,phone,type,time,time2,date_,address_,pic_address,equipid,comment,num_,isSuccess,isSend,sendId,sendName) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        int ins = jdbcTemplate.update(sql, new Object[]{d.getUserid(), d.getName(), d.getPhone(), d.getType(), d.getTime(), d.getTime2(),d.getDate_(), d.getAddress_(), d.getPic_address(), d.getEquipid(), d.getComment(),d.getNum_(),d.getIsSuccess(),d.getIsSend(),d.getSendId(),d.getSendName()});
        return ins>0;
    }

    /*
     *   jdbcTemplate.queryForObject("sql",new Object[]{"参数"},String.class);
     * */
    //查询num_这一列在当天中最大的数值
    public int checkMaxNumByDate_(String date2) {
        String sql="select max(num_) from dd_data where date_=?";
        String num_ = jdbcTemplate.queryForObject(sql, new Object[]{date2}, String.class);
        System.out.println(num_+",num_num_num_num_num_num_num_");
        if (num_==null){
            return 0;
        }else {
            return Integer.parseInt(num_);
        }
    }

    //查询当天需要写进钉钉考勤打卡的数据
    public List<Data> checkSendingData(String date, String isSend) {
        String sql="select * from dd_data where date_=? and isSend=?";
        List<Data> list = jdbcTemplate.query(sql,new Object[]{date,isSend}, new BeanPropertyRowMapper<Data>(Data.class));
        return list;
    }

    public int up_isSend(Integer id) {
        String sql="update dd_data set isSend=? where id=?";
        int up = jdbcTemplate.update(sql, new Object[]{"true", id});
        return up;

    }





}
